<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="68px">
      <el-form-item label="申诉编号" prop="code">
        <el-input
          v-model="queryParams.code"
          placeholder="请输入申诉编号"
          clearable
          size="small"
        />
      </el-form-item>
      <el-form-item label="订单号" prop="orderCode">
        <el-input
          v-model="queryParams.orderCode"
          placeholder="请输入订单号"
          clearable
          size="small"
        />
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" clearable size="small">
          <el-option label="待处理" :value="1" />
          <el-option label="已拒绝" :value="2" />
          <el-option label="已退款" :value="3" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="userOrderAppealList" >
      <!-- <el-table-column type="selection" width="55" align="center" /> -->
      <!-- <el-table-column label="主键" align="center" prop="id" /> -->
      <el-table-column label="申诉编号" align="center" prop="code" />
      <el-table-column label="订单号" align="center" prop="orderCode" />
      <el-table-column label="用户ID" align="center" prop="userId" />
      <el-table-column label="申诉理由" align="center" prop="reason" />
      <el-table-column label="截图" align="center" prop="screenshot" />
      <el-table-column label="状态" align="center" prop="status">
        <template slot-scope="scope">
          <span v-if="scope.row.status ===1 ">待处理</span>
          <span v-else-if="scope.row.status ===2 ">已拒绝</span>
          <span v-else-if="scope.row.status ===2 ">已退款</span>
          <span v-else>异常</span>
        </template>
      </el-table-column>
      <el-table-column label="提交时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span >{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="处理时间" align="center" prop="dealTime" width="180">
        <template slot-scope="scope">
          <span v-if="scope.row.dealTime">{{ parseTime(scope.row.dealTime) }}</span>
          <span v-else>--</span>
        </template>
      </el-table-column>
      <el-table-column label="操作人" align="center" prop="createBy" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" @click="handleUpdate(scope.row)">退款</el-button>
          <el-button size="mini" type="text" @click="handleUpdate(scope.row)">拒绝</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)" v-hasPermi="['system:userOrderAppeal:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改用户订单申诉对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="申诉编号" prop="code">
          <el-input v-model="form.code" placeholder="请输入申诉编号" />
        </el-form-item>
        <el-form-item label="订单号" prop="orderCode">
          <el-input v-model="form.orderCode" placeholder="请输入订单号" />
        </el-form-item>
        <el-form-item label="用户ID" prop="userId">
          <el-input v-model="form.userId" placeholder="请输入用户ID" />
        </el-form-item>
        <el-form-item label="申诉理由" prop="reason">
          <el-input v-model="form.reason" placeholder="请输入申诉理由" />
        </el-form-item>
        <el-form-item label="截图" prop="screenshot">
          <el-input v-model="form.screenshot" placeholder="请输入截图" />
        </el-form-item>
        <el-form-item label="状态(1待处理，2已拒绝，3已退款)">
          <el-radio-group v-model="form.status">
            <el-radio label="1">请选择字典生成</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="处理时间" prop="dealTime">
          <el-date-picker clearable size="small" style="width: 200px"
            v-model="form.dealTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择处理时间">
          </el-date-picker>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import { listUserOrderAppeal, getUserOrderAppeal, delUserOrderAppeal, addUserOrderAppeal, updateUserOrderAppeal, exportUserOrderAppeal } from "@/api/system/userOrderAppeal";

export default {
  name: "UserOrderAppeal",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,
      // 用户订单申诉表格数据
      userOrderAppealList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        code: undefined,
        orderCode: undefined,
        userId: undefined,
        reason: undefined,
        screenshot: undefined,
        status: undefined,
        dealTime: undefined,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        code: [
          { required: true, message: "申诉编号不能为空", trigger: "blur" }
        ],
        orderCode: [
          { required: true, message: "订单号不能为空", trigger: "blur" }
        ],
        userId: [
          { required: true, message: "用户ID不能为空", trigger: "blur" }
        ],
        reason: [
          { required: true, message: "申诉理由不能为空", trigger: "blur" }
        ],
        screenshot: [
          { required: true, message: "截图不能为空", trigger: "blur" }
        ],
        status: [
          { required: true, message: "状态(1待处理，2已拒绝，3已退款)不能为空", trigger: "blur" }
        ],
        createTime: [
          { required: true, message: "提交时间不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询用户订单申诉列表 */
    getList() {
      this.loading = true;
      listUserOrderAppeal(this.queryParams).then(response => {
        this.userOrderAppealList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        code: undefined,
        orderCode: undefined,
        userId: undefined,
        reason: undefined,
        screenshot: undefined,
        status: "0",
        createTime: undefined,
        dealTime: undefined,
        createBy: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!=1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加用户订单申诉";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getUserOrderAppeal(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改用户订单申诉";
      });
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != undefined) {
            updateUserOrderAppeal(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              }
            });
          } else {
            addUserOrderAppeal(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              }
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除用户订单申诉编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delUserOrderAppeal(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(function() {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有用户订单申诉数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportUserOrderAppeal(queryParams);
        }).then(response => {
          this.download(response.msg);
        }).catch(function() {});
    }
  }
};
</script>